from flask import url_for
from flask_restful import Resource

from model.crud import QueryOneInstance
from model.tables import User, Blog
from restful.data import DataPid
from restful.error import RenderUserNotExist, RenderPostNotExist
from restful.request import Get
from restful.response import ResponseSuccess
from restful.session import login_required, data


class AuthorDetail(Resource):
    @login_required
    @data(DataPid())
    def get(self, data):
        post=QueryOneInstance(Blog,{'id':data['pid']})

        if not post:
            return RenderPostNotExist(data['pid'])

        user = QueryOneInstance(User,{'id':post.uid})
        if not user:
            return RenderUserNotExist(data['uid'])

        userfollow_data=Get(url_for('userfollow',uid=user.id,_external=True))
        userlike_data=Get(url_for('userlike',uid=user.id,_external=True))
        userposts_data=Get(url_for('userposts',uid=user.id,_external=True))
        userdetail_data=Get(url_for('userdetail',uid=user.id,_external=True))

        return ResponseSuccess(data={
            **userdetail_data,
            **userfollow_data,
            **userlike_data,
            **userposts_data
        })